package com.wang.common.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import com.wang.common.util.DateUtil;
import com.wang.common.util.EncryptUtil;
import com.wang.common.util.PageInfoUtil;
import com.wang.common.util.RegexUtil;

/**
 * 所有Controller的公共代码放到这里面来
 * @author MyJJh
 *
 */
public class BaseController
{
	@Autowired
	protected DateUtil dateUtil;
	@Autowired
	protected EncryptUtil encryptUtil ;
	@Autowired
	protected RegexUtil regexUtil ; 
	
	/**
	 * 提示信息
	 */
	protected String info = "" ; 
	
	/**
	 * 生成搜索条件
	 * @param request
	 * @return
	 */
	protected Map<String, Object> proccedSearch(HttpServletRequest request)
	{
		/* 搜索条件 */
		Map<String,Object> condMap = new HashMap<>(1);
		/*====关键字开始====*/
		String keyword = request.getParameter("keyword");
		if(keyword == null)
		{
			keyword = "" ; 
		}
		keyword = keyword.trim() ; 
		condMap.put("keyword", keyword);
		request.setAttribute("keyword", keyword);
		/*====关键字结束====*/
		
		/*====状态开始====*/
		String status = request.getParameter("status");
		if(status == null)
		{
			status = "" ; 
		}
		condMap.put("status", status);
		request.setAttribute("status", status);
		/*====状态结束====*/
		
		/*====时间开始====*/
		Date stDate = null; 
		Date edDate = null ; 
		String st = request.getParameter("st");
		if(st == null)
		{
			st = "" ; 
		}
		String ed = request.getParameter("ed");
		if(ed == null)
		{
			ed = "" ; 
		}
		
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = this.dateUtil.strToDateTime(st) ; 
			edDate = this.dateUtil.strToDateTime(ed) ; 
		}
		/* 搜索条件 */
		condMap.put("st", stDate);
		condMap.put("ed", edDate);
		
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		return condMap;
	}

	/**
	 * 分页工具类
	 * @param request
	 * @return
	 */
	protected PageInfoUtil proccedPageInfoUtil(HttpServletRequest request)
	{
		/* 分页 */
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		
		try
		{
			String currentPage = request.getParameter("currentPage");
			String pageSize = request.getParameter("pageSize");
			pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
			pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
		} catch (NumberFormatException e)
		{
		}
		return pageInfoUtil;
	}
}
